/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Clases;

import java.util.ArrayList;

/**
 *
 * @author hC4stillo
 */
public class ModeloLineal {
    public Comun comun;
    public ArrayList<Object> x;
    public ArrayList<Object> y;
    public String r;
    public String rCuadrada;
    public String sexy;
    public ModeloLineal(ArrayList<Object> x, ArrayList<Object> y)
    {
        this.x=x;
        this.y=y;
        this.comun = new Comun(x,y);
    }
    public float sumatoriaX()
    {
        float sumX=0;
        for(int i = 0;i<this.x.size();i++)
            sumX=sumX+Float.parseFloat(this.x.get(i).toString());
        return sumX;
    }
    public float sumatoriaY()
    {
        float sumY=0;
        for(int i = 0;i<this.x.size();i++)
            sumY=sumY+Float.parseFloat(this.y.get(i).toString());
        return sumY;
    }
    public float sumatoriaXY()
    {
        float sumXY=0;
        ArrayList<Object> xy=this.comun.getXPorY();
        for(int i = 0;i<this.x.size();i++)
            sumXY=sumXY+Float.parseFloat(xy.get(i).toString());
        return sumXY;
    }
    public float sumatoriaXCuadrado()
    {
        ArrayList<Object> xCuadrado = this.comun.getXCuadrado();
        float sumXCuadrado=0;
        for(int i = 0;i<this.x.size();i++)
            sumXCuadrado=sumXCuadrado+Float.parseFloat(xCuadrado.get(i).toString());
        return sumXCuadrado;
    }
    public float sumatoriaYCuadrado()
    {
        ArrayList<Object> yCuadrado = this.comun.getYCuadrado();
        float sumYCuadrado=0;
        for(int i = 0;i<this.x.size();i++)
            sumYCuadrado=sumYCuadrado+Float.parseFloat(yCuadrado.get(i).toString());
        return sumYCuadrado;
    }
    public ArrayList<Object> getYCalculada()
    {
        ArrayList<Object> yCalculada=new ArrayList();
        float a = this.getA();
        float b = this.getB();
        for(int i=0;i<this.x.size();i++)
        {
            float temp= (a*Float.parseFloat(this.x.get(i).toString()))+b;
            yCalculada.add(temp);
        }
        return yCalculada;
    }
    public float getA()
    {
        float a=0;
        float part1 = (this.x.size()*this.sumatoriaXY())-(this.sumatoriaX()*this.sumatoriaY());
        float part2 = (float) ((this.x.size() * this.sumatoriaXCuadrado()) - (Math.pow(this.sumatoriaX(), 2)));
        a = part1/part2;
        return a;
    }
    public float getB()
    {
        float b=0;
        b=((this.sumatoriaY()/this.x.size())-(this.getA()*(this.sumatoriaX()/this.x.size())));
        return b;
    }
    /*
     * tabla de variacion
     */
    public float getVariacionTotal()
    {
        float variacionTotal=0;
        float promedioY = this.sumatoriaY()/this.y.size();
        for(int i =0; i<this.y.size();i++)
        {
            float temp = Float.parseFloat(this.y.get(i).toString())-promedioY;
            temp=(float) Math.pow(temp, 2);
            variacionTotal=variacionTotal+temp;
        }
        return variacionTotal;
    }
    public float getVariacionNoExplicada(ArrayList<Object> yCalculada)
    {
        float variacionNoExplicada=0;
        //ArrayList<Object> yCalculada = this.getYCalculada();
        for(int i =0; i<this.y.size();i++)
        {
            float temp = Float.parseFloat(this.y.get(i).toString())-Float.parseFloat(yCalculada.get(i).toString());
            temp=(float) Math.pow(temp, 2);
            variacionNoExplicada=variacionNoExplicada+temp;
        }
        return variacionNoExplicada;
    }
    public float getVariacionExplicada(ArrayList<Object> yCalculada)
    {
        float variacionExplicada=0;
        float promedioY = this.sumatoriaY()/this.y.size();
        //ArrayList<Object> yCalculada = this.getYCalculada();
        for(int i =0; i<this.y.size();i++)
        {
            float temp = Float.parseFloat(yCalculada.get(i).toString())-promedioY;
            temp=(float) Math.pow(temp, 2);
            variacionExplicada=variacionExplicada+temp;
        }
        return variacionExplicada;
    }
    public void calcularR(ArrayList<Object> yCalc)
    {
        float erreC = this.getVariacionExplicada(yCalc)/this.getVariacionTotal();
        float erre = (float) Math.sqrt(erreC);
        erre = this.comun.reducirDecimales(erre);
        erreC = this.comun.reducirDecimales(erreC*100);
        this.r = erre+"";
        this.rCuadrada = erreC+"%";
    }
    public void calcularSexy(ArrayList<Object> yCalc)
    {
        float temp = (float) Math.sqrt(this.getVariacionNoExplicada(yCalc)/(this.x.size()-2));
        temp = this.comun.reducirDecimales(temp);
        this.sexy = temp+"";
    }
}
